package com.investmenttechnologyservices.services;

import java.util.Date;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.inject.Inject;
import com.investmenttechnologyservices.model.Exchange;
import com.investmenttechnologyservices.model.InstrumentType;

public class ExchangeServiceImpl implements ExchangeService {
	private final DatastoreService ds;
	private final RegionService regionService; 
	
	@Inject
	ExchangeServiceImpl(DatastoreService ds, RegionService regionService) {
		this.ds = ds;
		this.regionService = regionService;
	}

	@Override
	public Date calculateSettlementDate(Key exchangeKey, Date tradeDate,
			InstrumentType instrumentType) {
		Date settlementDate = null;
		try {
			Exchange exchange = new Exchange(ds.get(exchangeKey));
			Key regionKey = exchange.getRegionKey();
			settlementDate = regionService.calculateSettlementDate(regionKey, tradeDate, instrumentType);
		} catch (EntityNotFoundException ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		}
		return settlementDate;
	}

}
